package iedcyt.acute.dataAccess.dao.maesorg;

import iedcyt.acute.dataAccess.domain.maesorg.WoS_REVISTAS_IdPub;
import iedcyt.acute.dataAccess.generic.SpringDao;
import iedcyt.acute.dataAccess.idao.maesorg.IWoS_REVISTAS_IdPubDao;

import java.util.List;

public class WoS_REVISTAS_IdPubDao extends SpringDao implements IWoS_REVISTAS_IdPubDao  {
	/**
	 * @see iedcyt.acute.dataAccess.idao.maesorg.Idbo_REVISTAS_JCRDao#create()
	 */
	@Override
	public void create() {
		//comprobamos si la tabla existe y si no la creamos
		try {
			_jdbcTemplate.queryForInt("select count(*) from [WoS REVISTAS IdPub]");
		} catch (Exception e) {
			String sql = "CREATE TABLE [WoS REVISTAS IdPub] (IdPub numeric, [ABV-corregida] varchar(255), PU varchar(255), SE varchar(255), ABV varchar(255), ISSN varchar(255))";
			_log.info(sql);
			_jdbcTemplate.execute(sql);
		}
	}
	
	@Override
	public void insert(WoS_REVISTAS_IdPub pub) {
	
		_jdbcTemplate.update("INSERT INTO [WoS REVISTAS IdPub] (IdPub, [ABV-corregida], PU, SE, ABV, ISSN) VALUES (?,?,?,?,?,?)",
				new Object[] { pub.get_idPub(), pub.get_ABV_corregida(), pub.get_PU(), pub.get_SE(),
				pub.get_ABV(), pub.get_ISSN() });
		
	}
	
	
	@Override
	public List<WoS_REVISTAS_IdPub> selectByIdPub(int idPub) {
		return _jdbcTemplate.query("SELECT * FROM [WoS REVISTAS IdPub] WHERE IdPub = ?",
				new Object[] { idPub },
				new WoS_REVISTAS_IdPubRowMapper());
	}

	@Override
	public List<WoS_REVISTAS_IdPub> selectByABV_corregida(String abv_corregida) {
		return _jdbcTemplate.query("SELECT * FROM [WoS REVISTAS IdPub] WHERE [ABV-corregida] = ?",
				new Object[] { abv_corregida},
				new WoS_REVISTAS_IdPubRowMapper());
	}

	@Override
	public List<WoS_REVISTAS_IdPub> selectAll() {
		return _jdbcTemplate.query("SELECT * FROM [WoS REVISTAS IdPub]",
				new WoS_REVISTAS_IdPubRowMapper());
	}

	@Override
	public List<WoS_REVISTAS_IdPub> selectByRange(int idPub1, int idPub2) {
		return _jdbcTemplate.query("SELECT * FROM [WoS REVISTAS IdPub] WHERE IdPub >= ? AND IdPub <= ?",
				new Object[] { idPub1, idPub2 },
				new WoS_REVISTAS_IdPubRowMapper());
	}

	@Override
	public int selectMaxIdPub() {
		return _jdbcTemplate.queryForInt("SELECT MAX(IdPub) FROM [WoS REVISTAS IdPub]");
	}

	@Override
	public List<WoS_REVISTAS_IdPub> selectByIssn(String issn) {
		return _jdbcTemplate.query("SELECT * FROM [WoS REVISTAS IdPub] WHERE ISSN = ?",
				new Object[] { issn },
				new WoS_REVISTAS_IdPubRowMapper());
	}

	@Override
	public List<WoS_REVISTAS_IdPub> selectByJournalLike(String jou) {
		
		String like = "%" + jou + "%"; 
		
		return _jdbcTemplate.query("SELECT * FROM [WoS REVISTAS IdPub] WHERE PU LIKE ?",
				new Object[] { like },
				new WoS_REVISTAS_IdPubRowMapper());
	}

	@Override
	public List<WoS_REVISTAS_IdPub> selectByPUBySEByABVByIssn(String PU,
			String SE, String ABV, String ISSN) {
		return _jdbcTemplate.query("SELECT * FROM [WoS REVISTAS IdPub] WHERE PU = ? AND SE = ? AND ABV = ? AND ISSN = ?",
				new Object[] { PU, SE, ABV, ISSN },
				new WoS_REVISTAS_IdPubRowMapper());
	}
	
	@Override
	public List<WoS_REVISTAS_IdPub> selectByPUByIssn(String PU, String ISSN) {
		return _jdbcTemplate.query("SELECT * FROM [WoS REVISTAS IdPub] WHERE PU = ? AND ISSN = ?",
				new Object[] { PU,ISSN },
				new WoS_REVISTAS_IdPubRowMapper());
	}
	
	@Override
	public List<WoS_REVISTAS_IdPub> selectByABVByIssn(String ABV, String ISSN) {
		return _jdbcTemplate.query("SELECT * FROM [WoS REVISTAS IdPub] WHERE ABV = ? AND ISSN = ?",
				new Object[] { ABV, ISSN },
				new WoS_REVISTAS_IdPubRowMapper());
	}


	@Override
	public void update(WoS_REVISTAS_IdPub pub) {
		_jdbcTemplate.update("UPDATE [WoS REVISTAS IdPub] SET [ABV-corregida] = ?, PU = ?, SE = ?, ABV = ?, ISSN  = ? WHERE IdPub = ?",
				new Object[] { pub.get_ABV_corregida(), pub.get_PU(), pub.get_SE(),
				pub.get_ABV(), pub.get_ISSN(), pub.get_idPub() });
	}


	@Override
	public List<WoS_REVISTAS_IdPub> selectByABVLike(String abv) {
		String like = "%" + abv + "%"; 
		
		return _jdbcTemplate.query("SELECT * FROM [WoS REVISTAS IdPub] WHERE ABV LIKE ?",
				new Object[] { like },
				new WoS_REVISTAS_IdPubRowMapper());
	}




}

